<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Produits_home_admin extends CI_Controller {

	function index()
	{
		$data['error'] ='display: none;';
		$data['home']  ='active';
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
			$data['title'] = 'Admin Home page';	
			$this->load->model('admin_model');
			$data['message'] = $this->admin_model->list_message();
			$data['results'] = $this->admin_model->home_service();				
			$data['content'] = 'admin/produits_home_admin';
			$this->load->view('admin/template/template',$data);	
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}
	
	
	function update()
	{
		if($this->session->userdata('email') || $this->session->userdata('logged'))
		{
			if($this->uri->segment(3))
			{
				$this->load->model('admin_model');
				$data['rows'] = $this->admin_model->list_update_service($this->uri->segment(3));
				// variable transmis à la vue
				//$data['id_service'] = $data['service']->id_service;
				$data['photos'] = $data['rows']->photos;
			
				$config['upload_path'] = './uploads/';
				$config['allowed_types'] = 'gif|png|jpg|jpeg';
				$config['max_size'] = '3048';
				$config['encrypt_name'] = true;
				$this->load->library('upload',$config);	
								
				if (!$this->upload->do_upload()) 
				{
					$this->lang->load('upload', $this->config->item('language'));
					$data['error_upload'] = $this->upload->display_errors();
					$data['title'] = 'Edit Produits et Service';
					$this->load->model('admin_model');
					$data['results'] = $this->admin_model->home_service();
					$data['message'] = $this->admin_model->list_message();			
					$data['content'] = 'admin/produits_home_admin';
					$this->load->view('admin/template/template',$data);	
				}
				else 
				{
					$IMAGE = $data['photos'];

					@unlink('uploads/'.$IMAGE);
					@unlink('uploads/thumbs/'.$IMAGE);
					
					$image_data = $this->upload->data();
					$config = array(
						'image_library' => 'GD2', 
						'source_image' => $image_data['full_path'],
						'new_image' => './uploads/thumbs/',
						'create_thumb' => TRUE,
						'thumb_marker' => '',
						'maintain_ratio' => FALSE,
						'width'=> 297,
						'height' => 181
					);
					
					$this->load->library('image_lib',$config);
					$this->image_lib->resize();
					$image_name = $image_data['file_name'];
					$this->load->model('admin_model');
					$data = array(
					'photos' => $image_name,
					);
					
					$this->load->model('crud_model');
					$this->crud_model->update_service_home($this->uri->segment(3),$data);
					$data['message'] = $this->admin_model->list_message();
					//-- supprimer image ancienne
					
					$this->load->model('admin_model');
					$data['results'] = $this->admin_model->home_service();
					$data['message'] = $this->admin_model->list_message();
					$data['content'] ='admin/produits_home_admin';
					$data['update'] = 'display: block;';
					$this->load->view('admin/template/template',$data);
					
				}
			}
			else 
			{
				redirect('produits_home_admin');	
			}
		}
		else 
		{
			$this->load->view('admin/signup',$data);
		}
	}
	
}

		
?>